<template>
  <a-modal
    title="编辑物料信息"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" :labelCol="labelCol" :wrapperCol="wrapperCol">

        <a-row>
          <a-col :span="12">
            <a-form-item label="物料编号" has-feedback>
              <a-input
                placeholder="请输入物料编号"
               disabled v-decorator="['materialNo', { rules: [{ required: true, message: '请输入物料编号！' }] }]"
              />
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="物料名称" has-feedback>
              <a-input
                placeholder="请输入物料名称"
                v-decorator="['materialName', { rules: [{ required: true, message: '请输入物料名称！' }] }]"
              />
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="物料类别">
              <a-select
                style="width: 100%"
                placeholder="请选择物料类别"
                v-decorator="['materialType', { rules: [{ required: true, message: '请选择物料类别！' }] }]"
              >
                <a-select-option v-for="(item, index) in materialTypeData" :key="index" :value="item.code">{{
                  item.name
                }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="物料尺寸" has-feedback>
              <a-input placeholder="请输入物料尺寸" v-decorator="['materialSpec']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="物料密度" has-feedback>
              <a-input placeholder="请输入物料密度" v-decorator="['materialDensity']" />
            </a-form-item>
          </a-col>

          <!-- <a-form-item label="库区参数"  has-feedback>
          <a-select mode="multiple" style="width: 100%" placeholder="请选择库区参数" v-decorator="['areas']">
            <a-select-option v-for="(item,index) in areaNameParameterData" :key="index" :value="item.areaId">{{ item.areaName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="工作区参数"  has-feedback>
          <a-select mode="multiple" style="width: 100%" placeholder="请选择工作区参数" v-decorator="['stations']">
            <a-select-option v-for="(item,index) in stationNameParameterData" :key="index" :value="item.stationId">{{ item.stationName }}</a-select-option>
          </a-select>
        </a-form-item> -->

          <a-col :span="12">
            <a-form-item label="物料批次" has-feedback>
              <a-input
              disabled  placeholder="请输入物料批次"
                v-decorator="['materialBatch', { rules: [{ required: true, message: '请输入物料批次！' }] }]"
              />
            </a-form-item>
          </a-col>

          <a-col :span="12">
            <a-form-item label="物料检验" has-feedback>
              <a-select
                style="width: 100%"
                placeholder="请选择物料检验"
                v-decorator="['inspectionMethod', { rules: [{ required: true, message: '请选择物料检验！' }] }]"
              >
                <a-select-option v-for="(item, index) in inspectionMethodData" :key="index" :value="item.code">{{
                  item.name
                }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="物料单位" has-feedback>
              <a-input placeholder="请输入物料单位" v-decorator="['materialUnit']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="安全存量" has-feedback>
              <a-input type="number" min="1" placeholder="请输入安全存量" v-decorator="['safeqty']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="最大存量" has-feedback>
              <a-input type="number" min="1" placeholder="请输入最大存量" v-decorator="['maxImumqty']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="最小库龄" has-feedback>
              <a-input type="number" min="1" placeholder="请输入最小库龄" v-decorator="['minstorageAge']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="最大库龄" has-feedback>
              <a-input type="number" min="1" placeholder="请输入最大库龄" v-decorator="['maxstorageAge']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="描述" has-feedback>
              <a-textarea :rows="4" placeholder="请输入描述" v-decorator="['description']"></a-textarea>
            </a-form-item>
          </a-col>
        </a-row>
        <!-- <a-form-item label="熟化时间" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input-number placeholder="请输入熟化时间" style="width: 100%" v-decorator="['maturationTime']" />
        </a-form-item>
        <a-form-item label="是否需要熟化" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-switch v-decorator="['isMaturation', { valuePropName: 'checked' }]" />
        </a-form-item> -->
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {
    WmsMaterialEdit,GetAreas,GetStations
  } from '@/api/modular/main/WmsMaterialManage'
  export default {
    data () {
      return {
	    Id: 0,
      labelCol: {
        // xs: { span: 24 },
        // sm: { span: 5 }
        span: 8
      },
      wrapperCol: {
        // xs: { span: 24 },
        // sm: { span: 15 }
        span: 13
      },
        record: {},
        inspectionMethodData: [],
        materialTypeData: [],
        areaNameParameterData: [],
        stationNameParameterData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      edit (record) {
        this.visible = true;
        this.Id = record.id;
        this.$nextTick(() => {
          this.getSelects()
        });
        //深度拷贝 移除VUE的监听，防止INDEX页面值变动
        this.record = JSON.parse(JSON.stringify(record))
        const materialTypeOption = this.$options
        this.materialTypeData = materialTypeOption.filters['dictData']('material_type')
        const inspectionMethodOption = this.$options
        this.inspectionMethodData = inspectionMethodOption.filters['dictData']('material_inspection')

        this.$nextTick(() => {
          let {areas, stations, ...rest } = record
          this.form.setFieldsValue(
            {
              ...rest,
              // id: record.id,
              // materialNo: record.materialNo,
              // materialName: record.materialName,
              // materialType: record.materialType,
              // materialSpec: record.materialSpec,
              // materialDensity: record.materialDensity,
              // maxImumqty: record.maxImumqty,
              areas: record.areaIds?record.areaIds.split(','):[],
              stations: record.stationIds?record.stationIds.split(','):[],
              // maturationTime: record.maturationTime,
              // isMaturation: record.isMaturation
            }
          )
        })
      },
      getSelects(){
        if (this.areaNameParameterData.length<=0 || this.stationNameParameterData.length<=0) {
          this.confirmLoading = true
          Promise.all([
            GetAreas(),
            GetStations()
          ]).then(response=>{
            this.areaNameParameterData = response[0].data || []
            this.stationNameParameterData = response[1].data || []
            this.confirmLoading = false
          }).catch(()=>{
            this.confirmLoading = false
          })
        }
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            let params = {...this.record,...values}
            if (this.$getObjectType(values.areas)==='array') {
              params.areaIds = values.areas.join(',')
              params.areaNameParameterArr = values.areas.map((val)=>{
                return this.parseSelectName(val,'areaId','areaName','areaNameParameterData')
              })
              params.areaNameParameter = params.areaNameParameterArr.join(',')
            }
            if (this.$getObjectType(values.stations)==='array'){
               params.stationIds = values.stations.join(',')
               params.stationNameParameterArr = values.stations.map((val)=>{
                 return this.parseSelectName(val,'stationId','stationName','stationNameParameterData')
               })
               params.stationNameParameter = params.stationNameParameterArr.join(',')
            }
            
            WmsMaterialEdit(params).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', params)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败：' + JSON.stringify(res.message))
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          }else{
            this.confirmLoading = false
          } 
        });
      },
      parseSelectName(val,field,nfield,pfield){
        let res = val;
        for (let i=0;i<this[pfield].length;i++) {
          if (val===this[pfield][i][field]) {
            res = this[pfield][i][nfield]
            break
          }
        }
        return res
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
